//
//  TMGHelpTableViewCell.m
//  TMGold
//
//  Created by Mac on 2017/12/11.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGDiscountsTableViewCell.h"
@interface  TMGDiscountsTableViewCell()
@property(nonatomic, strong) UIView                     *backGroundView;

@property(nonatomic, strong) STLabel                     *timeLable;

@end

@implementation TMGDiscountsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self configSubView];
    }
    return self;
}
+ (CGFloat)cellHeight{
    return 135;
}
#pragma mark --subView
- (void)configSubView{
    self.backgroundColor = TM_backgroundColor;
    
    self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(15, 10, UIScreenWidth - 30, 110)];
    self.backGroundView.backgroundColor = TM_orangeColor;
    self.backGroundView.clipsToBounds = YES;
    self.backGroundView.layer.cornerRadius = 10;
    [self addSubview:self.backGroundView];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(15, 20, self.backGroundView.width, self.backGroundView.height - 10)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:whiteView];
    self.titleLable = [[STLabel alloc] initWithFrame:CGRectMake(5, 15, 300, 25)
                                                text:@"￥23    仅可以用于买金类"
                                           textColor:TM_firstTextColor
                                                font:13
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentLeft];
    self.titleLable.attributedText = [self.titleLable.text st_convertAttributeStringWithKeyWord:@"￥23" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:TM_redColor}];
    [whiteView addSubview:self.titleLable];
    
    self.useButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 80, 44)
                                                title:@"立即领取"
                                           titleColor:TM_orangeColor
                                            titleFont:13
                                         cornerRadius:0
                                      backgroundColor:nil
                                      backgroundImage:nil
                                                image:nil];
    self.useButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

    self.useButton.right = whiteView.width - 15;
    self.useButton.centerY = self.titleLable.centerY;
    [whiteView addSubview:self.useButton];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, self.titleLable.bottom + 20 , self.backGroundView.width, 0.5)];
    line.backgroundColor = TM_lineColor;
    [whiteView addSubview:line];
    
    self.timeLable = [[STLabel alloc] initWithFrame:CGRectMake(5, line.bottom , self.backGroundView.width - 30, 45)
                                              text:@"有效期至2017-12-11 09:42:08"
                                         textColor:TM_secendTextColor
                                              font:14
                                       isSizetoFit:NO
                                      textAlignment:NSTextAlignmentLeft];

    [whiteView addSubview:self.timeLable];
    
}
- (void)setModel:(TMGDiscountModel *)model{
    _model = model;
   // 时间判断 可用
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [formatter setTimeZone:GTMzone];
    NSDate * date = [formatter dateFromString:model.end];
    NSInteger num = [date timeIntervalSince1970];
    NSInteger now = [[NSDate st_localCurrentDate] timeIntervalSince1970];
    if (num >= now && !model.is_used.boolValue) {
        self.titleLable.textColor = TM_redColor;
        self.timeLable.textColor = FirstTextColor;
        self.backGroundView.backgroundColor = TM_orangeColor;
    }else{
        self.titleLable.textColor = ThirdTextColor;
        self.timeLable.textColor = ThirdTextColor;
        self.backGroundView.backgroundColor = [UIColor colorWithRed:214/255.0 green:214/255.0 blue:214/255.0 alpha:1];
    }
    
    self.titleLable.text = [NSString stringWithFormat:@"%@",model.title];
    self.timeLable.text = [NSString stringWithFormat:@"有效期至%@",
                           model.end];
    if (self.model.is_used.boolValue) {
        [self.useButton setTitle:@"已经领取" forState:UIControlStateNormal];
        self.useButton.userInteractionEnabled = NO;
    }else{
        [self.useButton setTitle:@"立即领取" forState:UIControlStateNormal];
        self.useButton.userInteractionEnabled = YES;
    }
}
@end

